package com.xzlf.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.injector.methods.SelectById;
import com.xzlf.config.JobTaskConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzlf.mapper.JobTaskMapper;
import com.xzlf.domain.JobTask;
import com.xzlf.service.JobTaskService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.annotation.ApplicationScope;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Service
@Slf4j
public class JobTaskServiceImpl extends ServiceImpl<JobTaskMapper, JobTask> implements JobTaskService{

//    @Autowired
//    private Map<String, JobTask> jobTaskMap;

    @Autowired
    private JobTaskMapper jobTaskMapper;

    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public void updateCron(JobTask jobTask) {
        updateById(jobTask);
        JobTask task = jobTaskMapper.selectById(jobTask.getId());
        Map<String, JobTask> jobTaskMap = (Map<String, JobTask>) applicationContext.getBean("jobTaskMap");
        jobTaskMap.remove(task.getJobName());
        jobTaskMap.put(task.getJobName(), task);
       /* //获取BeanFactory
        DefaultListableBeanFactory defaultListableBeanFactory
                = (DefaultListableBeanFactory) applicationContext.getAutowireCapableBeanFactory();
        //创建bean信息.
        BeanDefinitionBuilder jobTaskMap = BeanDefinitionBuilder.genericBeanDefinition("jobTaskMap");
        //动态注册bean.
        defaultListableBeanFactory.registerSingleton("jobTaskMap",map);*/
        log.info("替换后的map:{}", JSON.toJSONString(jobTaskMap, true));
    }
}
